package direccionamiento.frame;

//~--- JDK imports ------------------------------------------------------------

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import javax.swing.border.LineBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.InetAddress;

/**
 * <b>Class</b> DireccionamientoFrame.java
 *
 * <p><b>Description:<b> ...</p>
 * <p><b>Copyright:<b> Miguel Monterrey © 2012</p>
 * <p><b>Enterprise:<b> ULL</p>
 *
 * @version        1.0, 04/10/2012
 * @author         Miguel Monterrey    
 */


public class DireccionamientoFrame {

    /** Field description */
    private JFrame frame;

    /**
     * Create the application.
     */
    public DireccionamientoFrame() {
        initialize();
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    DireccionamientoFrame window = new DireccionamientoFrame();

                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setBackground(new Color(0, 51, 102));
        frame.setBounds(100, 100, 500, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        JPanel panelFooter = new JPanel();
        panelFooter.setBorder(new LineBorder(new Color(0, 0, 0), 3));
        panelFooter.setBackground(Color.DARK_GRAY);
        panelFooter.setBounds(0, 327, 500, 51);
        frame.getContentPane().add(panelFooter);
        panelFooter.setLayout(null);
        
        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.aquaVariant"), "Direccionamiento", TitledBorder.CENTER, TitledBorder.TOP, new Font("Consolas", Font.BOLD, 19), new Color(0, 0, 0)));
        panel.setBackground(new Color(0, 51, 102));
        panel.setBounds(5, 6, 490, 316);
        frame.getContentPane().add(panel);
        panel.setLayout(null);
        
        JLabel lblHost = new JLabel("Introduce el Hostname:");
        lblHost.setFont(new Font("Consolas", Font.BOLD, 17));
        lblHost.setBounds(25, 82, 225, 16);
        panel.add(lblHost);
        
        JLabel lblDirIP = new JLabel("La direcci\u00F3n IP es:");
        lblDirIP.setFont(new Font("Consolas", Font.BOLD, 17));
        lblDirIP.setBounds(25, 143, 225, 24);
        panel.add(lblDirIP);
        
        JLabel lblNewLabel = new JLabel("La direcci\u00F3n IP local es:");
        lblNewLabel.setFont(new Font("Consolas", Font.BOLD, 17));
        lblNewLabel.setBounds(25, 211, 225, 16);
        panel.add(lblNewLabel);
        
        final JTextPane txtpnError = new JTextPane();
        txtpnError.setFont(new Font("Consolas", Font.BOLD, 20));
        txtpnError.setBackground(new Color(0, 51, 102));
        txtpnError.setBounds(25, 257, 438, 30);
        panel.add(txtpnError);
        
        final JTextField txtHost = new JTextField();
        txtHost.setHorizontalAlignment(SwingConstants.CENTER);
        txtHost.setBackground(new Color(0, 102, 153));
        txtHost.setFont(new Font("Consolas", Font.PLAIN, 13));
        txtHost.setBounds(262, 74, 205, 24);
        panel.add(txtHost);
        
        final JTextField textIP = new JTextField();
        textIP.setHorizontalAlignment(SwingConstants.CENTER);
        textIP.setBackground(new Color(51, 102, 153));
        textIP.setFont(new Font("Consolas", Font.PLAIN, 13));
        textIP.setBounds(262, 143, 205, 24);
        panel.add(textIP);
        
        final JTextField textIPLocal = new JTextField();
        textIPLocal.setHorizontalAlignment(SwingConstants.CENTER);
        textIPLocal.setBackground(new Color(102, 102, 153));
        textIPLocal.setFont(new Font("Consolas", Font.PLAIN, 13));
        textIPLocal.setBounds(262, 207, 201, 24);
        panel.add(textIPLocal);
        
        JButton btnTranslate = new JButton("Traducir");
        
        btnTranslate.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		try {
        			if (txtHost.getText().equals(""))
        				txtHost.setText("Especifica un hostname");
        			else {
        				// Devuelve la InetAddress de un anfitrión especificado mediante un
        				// nombre (hostname)
        				InetAddress addr = InetAddress.getByName(txtHost.getText());
        				textIP.setText(addr.toString());
        			
        				// Devuelve una InetAddress para el anfitrión local
        				InetAddress addr2 = InetAddress.getLocalHost();
        				textIPLocal.setText(addr2.toString());
        			}
        		}
        		catch (Exception ev) {
        			txtpnError.setText("          Error de Excepción");
        			txtpnError.setBackground(Color.RED);
        		}
        	}
        });
        
        btnTranslate.setHorizontalAlignment(SwingConstants.LEADING);
        btnTranslate.setBackground(new Color(0, 51, 51));
        btnTranslate.setFont(new Font("Consolas", Font.BOLD, 16));
        btnTranslate.setBounds(188, 6, 117, 39);
        panelFooter.add(btnTranslate);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
